package com.platform.qujia.controller;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.config.QuJiaConfig;
import com.platform.qujia.utils.FileUploadUtils;
import com.platform.qujia.utils.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/common")
@CrossOrigin
@Slf4j
public class CommonController {

    @Value("${domain.adminName}")
    private String FILE_UPLOAD;

    /**
     * 用不了
     * */
    @PostMapping("/uploadFile")
    public ResultData uploadFile(MultipartFile file){
        log.info("uploadFile 上传文件：[{}]", file);
        try {

            // 上传文件路径
            String filePath = QuJiaConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            log.info("----===============fileName:[{}]", fileName);
            log.info("----===============getImageurl:[{}]", QuJiaConfig.getImageUrl());
            String url = QuJiaConfig.getImageUrl() + fileName;
            Map<String,Object> map = new HashMap<>();
            map.put("fileName",fileName);
            map.put("url",url);
            return ResultData.success(map);
        }
        catch (Exception e)
        {
            log.info(e.toString());
            return ResultData.error(e.getMessage());
        }
    }


    /**
     * 调用admin项目上传文件
     * */
    @PostMapping("/admin/uploadFile")
    public ResultData adminUploadFile(@RequestPart(value = "files",required = false) MultipartFile[] files){
        log.info("调用admin项目上传文件 上传文件：[{}]",files);
        if(Objects.isNull(files)){
            return ResultData.success();
        }
        List<String> list = new ArrayList<>();
        for (MultipartFile multipartFile : files) {
            try {
                File file = new File("../src/main/resources/input.txt");
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);

                log.info("调用admin项目地址：[{}]",FILE_UPLOAD + "/prod-api/common/uploadFile");
                HttpResponse response = HttpUtil.createPost(FILE_UPLOAD + "/prod-api/common/uploadFile").form("file", file).timeout(5000).execute();
                log.info("调用admin项目返回结果：[{}]",response.body());
                JSONObject jsonObject = JSON.parseObject(response.body());
                String url = jsonObject.getString("url");
                log.info("支付项目上传文件地址：[{}]",url);
                list.add(url);
            } catch (IOException e){
                log.info("MultipartFile 转换File 异常：[{}]",e.getMessage());
                return ResultData.error("文件转换失败");
            }
        }
        return ResultData.success(list);
    }

}
